﻿
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Xml;
using System.Linq;


namespace AFanDa.Common
{


    /// <summary>
    /// 配置文件操作类
    /// </summary>
    public class Config
    {
        /// <summary>
        /// 获取配置文件
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetKey(string key)
        {
            string val = ConfigurationManager.AppSettings[key];
            return val ?? "";
        }

        static Random rd = new Random();
        /// <summary>
        /// 获取随机数
        /// </summary>
        /// <param name="min">最小值 包含</param>
        /// <param name="max">最大值 不包含</param>
        /// <returns></returns>
        public static int GetRandom(int min, int max)
        {
            return rd.Next(min, max);
        }

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStamp()
        {

            return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
        }




        /// <summary>
        /// 获取客户端IP地址
        /// </summary>
        /// <returns>若失败则返回回送地址</returns>
        public static string GetIP()
        {
            return "127.0.0.1";
        }


        /// <summary>
        /// 检查IP地址格式
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIP(string ip)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }





        /// <summary>
        /// 生成GUID
        /// </summary>
        /// <returns></returns>
        public static string GUID()
        {
            return Guid.NewGuid().ToString().Replace("-", "").ToUpper();
        }
    }
}
